package com.ruoyi.web.controller.resourcepool;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysDataScope;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.DO.RetrievalFacetField;
import com.ruoyi.system.domain.DO.RetrievalSearchField;
import com.ruoyi.system.domain.DO.RetrievalSearchParams;
import com.ruoyi.system.domain.SysCommonDataProperty;
import com.ruoyi.system.domain.SysDataProperty;
import com.ruoyi.system.protocol.IDynamicCacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

public class BaseRetrievalController extends BaseController {


    private static final Logger LOGGER = LoggerFactory.getLogger(BaseRetrievalController.class);

    @Autowired
    private IDynamicCacheService dynamicCache;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 获取分面字段
     * @param retrievalSearchParams 查询实体
     */
    public void getFacetField(RetrievalSearchParams retrievalSearchParams) {
        List<RetrievalFacetField> facetFieldsList = new ArrayList<>();
        List<String> appointFacetField = retrievalSearchParams.getAppointFacetField();
        Object classesEnObj = retrievalSearchParams.getClassesEn();
        List<String> classesEnList = new ArrayList<>();
        if(classesEnObj instanceof String){
            classesEnList.add((String) classesEnObj);
        }else if(classesEnObj instanceof List<?>){
            classesEnList = (List<String>) classesEnObj;
        }
        RetrievalFacetField retrievalFacetField = null;
        if(classesEnList != null && classesEnList.size() > 0){
            List<SysDataProperty> sysDataPropertyListAll = new ArrayList<>();
            List<SysDataProperty> dataPropertyListCache = null;
            List<SysDataProperty> sysDataPropertyList = null;
            for (String classesEn : classesEnList) {
                dataPropertyListCache = dynamicCache.getDataPropertyListCache(classesEn);
                sysDataPropertyList = dataPropertyListCache.stream()
                        .filter(x -> "1".equals(x.getIndexed()) && "1".equals(x.getFaceted()))
                        .collect(Collectors.toList());
                sysDataPropertyListAll.addAll(sysDataPropertyList);
            }
            for (SysDataProperty sysDataProperty : sysDataPropertyListAll) {
                retrievalFacetField = new RetrievalFacetField();
                retrievalFacetField.setNameEn(sysDataProperty.getNameEn());
                retrievalFacetField.setDictId(sysDataProperty.getDictId()+"");
                retrievalFacetField.setRetrievalName(dynamicCache.getFiledForward(sysDataProperty.getNameEn()));
                retrievalFacetField.setNameZh(sysDataProperty.getNameZh());
                retrievalFacetField.setDataTypeName(sysDataProperty.getDataTypeName());
                facetFieldsList.add(retrievalFacetField);
            }
        }else{
            List<SysCommonDataProperty> commonDataPropertyListCache = dynamicCache.getCommonDataPropertyListCache();
            List<SysCommonDataProperty> sysCommonDataProperties = commonDataPropertyListCache.stream()
                    .filter(x -> "1".equals(x.getIndexed()) && "1".equals(x.getFaceted()))
                    .collect(Collectors.toList());
            for (SysCommonDataProperty commonDataProperty : sysCommonDataProperties) {
                retrievalFacetField = new RetrievalFacetField();
                retrievalFacetField.setNameEn(commonDataProperty.getNameEn());
                retrievalFacetField.setDictId(commonDataProperty.getDictId()+"");
                retrievalFacetField.setRetrievalName(dynamicCache.getFiledForward(commonDataProperty.getNameEn()));
                retrievalFacetField.setNameZh(commonDataProperty.getNameZh());
                retrievalFacetField.setDataTypeName(commonDataProperty.getDataTypeName());
                facetFieldsList.add(retrievalFacetField);
            }
        }
        if(appointFacetField != null && appointFacetField.size() > 0){
            facetFieldsList = facetFieldsList.stream().filter(x -> appointFacetField.contains(x.getNameEn())).collect(Collectors.toList());
        }
        retrievalSearchParams.setFacetFields(facetFieldsList);
    }


    /**
     * 获取查询字段
     * @param retrievalSearchParams 查询实体
     */
    public void getSearchField(RetrievalSearchParams retrievalSearchParams) {
        List<RetrievalSearchField> retrievalSearchFieldsList = new ArrayList<>();
        RetrievalSearchField retrievalSearchField = null;
        Object classesEnObj = retrievalSearchParams.getClassesEn();
        List<String> classesEnList = new ArrayList<>();
        if(classesEnObj instanceof String){
            classesEnList.add((String) classesEnObj);
        }else if(classesEnObj instanceof List<?>){
            classesEnList = (List<String>) classesEnObj;
        }
        if(classesEnList != null && classesEnList.size() > 0){
            List<SysDataProperty> dataPropertyListCache = null;
            for (String classesEn : classesEnList) {
                dataPropertyListCache = dynamicCache.getDataPropertyListCache(classesEn);
                for (SysDataProperty sysDataProperty : dataPropertyListCache) {
                   if("1".equals(sysDataProperty.getIndexed()) && "1".equals(sysDataProperty.getGeneralSearchField())){
                       retrievalSearchField = new RetrievalSearchField();
                       retrievalSearchField.setNameEn(sysDataProperty.getNameEn());
                       retrievalSearchField.setRetrievalName(sysDataProperty.getSearchNameEn());
                       retrievalSearchField.setParticiple(sysDataProperty.getParticiple());
                       retrievalSearchField.setHighlightingField(sysDataProperty.getHighlightingField());
                       retrievalSearchField.setDataTypeName(sysDataProperty.getDataTypeName());
                       retrievalSearchField.setNameZh(sysDataProperty.getNameZh());
                       retrievalSearchFieldsList.add(retrievalSearchField);
                   }
                }
            }
        }else{
            List<SysCommonDataProperty> commonDataPropertyListCache = dynamicCache.getCommonDataPropertyListCache();
            for (SysCommonDataProperty commonDataProperty : commonDataPropertyListCache) {
                if("1".equals(commonDataProperty.getIndexed()) && "1".equals(commonDataProperty.getGeneralSearchField())){
                    retrievalSearchField = new RetrievalSearchField();
                    retrievalSearchField.setNameEn(commonDataProperty.getNameEn());
                    retrievalSearchField.setRetrievalName(commonDataProperty.getSearchNameEn());
                    retrievalSearchField.setParticiple(commonDataProperty.getParticiple());
                    retrievalSearchField.setHighlightingField(commonDataProperty.getHighlightingField());
                    retrievalSearchField.setDataTypeName(commonDataProperty.getDataTypeName());
                    retrievalSearchField.setNameZh(commonDataProperty.getNameZh());
                    retrievalSearchFieldsList.add(retrievalSearchField);
                }
            }
        }
        retrievalSearchParams.setSearchFields(retrievalSearchFieldsList);
    }

    /**
     * 获取详情字段
     * @param retrievalSearchParams 查询实体
     */
    public void getDetailField(RetrievalSearchParams retrievalSearchParams) {
        List<RetrievalSearchField> retrievalDetailFieldsList = new ArrayList<>();
        RetrievalSearchField retrievalSearchField = null;
        Object classesEnObj = retrievalSearchParams.getClassesEn();
        List<String> classesEnList = new ArrayList<>();
        if(classesEnObj instanceof String){
            classesEnList.add((String) classesEnObj);
        }else if(classesEnObj instanceof List<?>){
            classesEnList = (List<String>) classesEnObj;
        }
        if(classesEnList != null && classesEnList.size() > 0){
            List<SysDataProperty> dataPropertyListCache = null;
            for (String classesEn : classesEnList) {
                dataPropertyListCache = dynamicCache.getDataPropertyListCache(classesEn);
                dataPropertyListCache = dataPropertyListCache.stream().sorted(Comparator.comparing(SysDataProperty::getSort)).collect(Collectors.toList());
                for (SysDataProperty sysDataProperty : dataPropertyListCache) {
                    if("1".equals(sysDataProperty.getWebDetailShow())){
                        retrievalSearchField = new RetrievalSearchField();
                        retrievalSearchField.setNameEn(sysDataProperty.getNameEn());
                        retrievalSearchField.setNameZh(sysDataProperty.getNameZh());
                        retrievalSearchField.setDataTypeName(sysDataProperty.getDataTypeName());
                        retrievalDetailFieldsList.add(retrievalSearchField);
                    }
                }
            }
        }
        retrievalSearchParams.setListDisplayFields(retrievalDetailFieldsList);
    }

    /**
     * 处理字符串前缀
     * @param classesEn
     * @param dynamic
     * @param type enter/out
     */
    public void handleFilePrefix(String classesEn, Map<String, Object> dynamic, String type){
        String urlPrefix = serverConfig.getUrl();
        List<SysDataProperty> dataPropertyListCache = dynamicCache.getDataPropertyListCache(classesEn);
        for(String key: dynamic.keySet()){
            SysDataProperty sysDataProperty = dataPropertyListCache.stream().filter(x -> x.getNameEn().equals(key)).findFirst().orElse(null);
            if(sysDataProperty != null && sysDataProperty.getDataTypeId() == 3){ //如果为富文本框
                String val = dynamic.get(key) + "";
                if("enter".equals(type)){
                    val = val.replace("src=\""+urlPrefix, "src=\"${dynamic_prefix}");
                }else{
                    val = val.replace("src=\"${dynamic_prefix}", "src=\""+urlPrefix);
                }
                dynamic.put(key, val);
            }
        }
    }

}
